<?
/*
 Plugin Name: Course Management
Description: Plugin manages courses
Author: Maxat, Karthick, Sevgi
Version: 1.0
*/
?>

<?
include_once("user-photo.php");

$homeURL =  get_home_url();

$db_name = DB_NAME;
//include_once("../wp-config.php");
//echo DB_NAME;
global $db_name;
//$_SESSION["maxat"] = "maxat11";
//$max = "per";
//echo $GLOBALS["max"];
//echo DB_NAME;
//print_r( get_defined_vars() );

?>

<link rel="stylesheet"
	href="<?echo $homeURL?>/wp-content/plugins/course_management/styles/style.css"
	type="text/css">
<script
	type="text/javascript"
	src="<?echo $homeURL?>/wp-content/plugins/course_management/js/jquery-1.6.1.min.js"></script>
<script
	type="text/javascript"
	src="<?echo $homeURL?>/wp-content/plugins/course_management/js/javascript.js"></script>

<?
error_reporting(0);
add_action( 'admin_menu', 'my_plugin_menu' );

function my_plugin_menu() {
	add_options_page( 'My Plugin Options', 'Course Management', 'manage_options', 'my-unique-identifier', 'my_plugin_options' );
}

function my_plugin_options() {
	/*if ( !current_user_can( 'manage_options' ) )  {
		wp_die( __( 'You do not have sufficient permissions to access this page.' ) );
	}
	echo '<div class="wrap">';
	echo '<p>Here is where the form would go if I actually had options.</p>';
	echo '</div>';*/
	global $wpdb;
    ?>
    <br>
    <br>
 <form>
  <fieldset>
    <table>
      <tbody>
        <tr>
          <td><button id='table1' onclick='display_table(1,0);return false;' style="width:140; border: 4px solid #DCDCDC; background: #EFEFFC none repeat scroll 0%; font-weight:bold; text-align:center;">Courses</button></td>
          <td><button id='table2' onclick='display_table(2,0);return false;' style="width:180; border: 4px solid #DCDCDC; background: #EFEFFC none repeat scroll 0%; font-weight:bold; text-align:center;">Lectures</button></td>
          <td><button id='table3' onclick='display_table(3,0);return false;' style="width:140; border: 4px solid #DCDCDC; background: #EFEFFC none repeat scroll 0%; font-weight:bold; text-align:center;">Exercises</button></td>
          <!--<td><button id='table4' onclick='display_table(4,0);return false;' style="width:180; border: 4px solid #DCDCDC; background: #EFEFFC none repeat scroll 0%; font-weight:bold; text-align:center;">Teaching Staff</button></td>
          <td><button id='table5' onclick='display_table(5,0);return false;' style="width:180; border: 4px solid #DCDCDC; background: #EFEFFC none repeat scroll 0%; font-weight:bold; text-align:center;">Additional</button></td>-->
				</tr>
			</tbody>
		</table>
		<table width="100%" border="0" cellspacing="0" cellpadding="0">
			<tbody>
				<tr>
					<td height="22">
						<center>
							<div id="loading"
								style="display: none; font-size: 18; background: #FFF1A8; width: 90px;">Loading...</div>
						</center>
					</td>
				</tr>
			</tbody>
		</table>
	</fieldset>
</form>
<div id="tab"
	style="width: 100%; height: 100%;"></div>



<?
}

/*Adding my own widget Maxat*/
//error_reporting(E_ALL);
add_action("widgets_init", array('Widget_name', 'register'));
class Widget_name {
	function control(){
		echo 'I am a control panel<br>';
	}
	function widget($args){
		//echo $args['before_widget'];
		//echo $args['before_title'] . 'Your widget title' . $args['after_title'];
		//echo 'I am your widget';
		//echo $args['after_widget'];
		global $wpdb;
		//global $m_course_id;
		$courseResults = $wpdb->get_results("SELECT * FROM g_courses WHERE year=".date("Y"));
		$archiveCourseResults = $wpdb->get_results("SELECT * FROM g_courses WHERE year<".date("Y"));
		$users = $wpdb->get_results("SELECT * FROM wp_users");
		?>
<div class="sidebar-wrap clearfix widget_categories">
<h3>Course Management</h3>
<ul>
	<!--Displaying current semester courses-->
	<?      
	foreach($courseResults as $course){
		//echo "<a href='".$result->page_url."' onclick='course_click('".$result->page_url."',".$result->course_id.");'>".$result->name."</a><br>";
		?>

	<!-- Displaying courses -->
	<li><a
		href="<?echo $homeURL.'?cid='.$course->course_id.'&cmd=getCourse'?>"><?echo $course->name?>
	</a> <?
	$selectedId= $_GET['cid'];
	if(!is_null($selectedId) && $selectedId==$course->course_id)
	{
		?>
		<ul>
			<li><a href="<?echo $homeURL.'?cid='.$selectedId.'&cmd=getLecture'?>">Lectures</a>
			</li>
			<li><a
				href="<?echo $homeURL.'?cid='.$selectedId.'&cmd=getExercise'?>">Exercises</a>
			</li>
		</ul> <?
	}
	?>
	</li>
	<?
	}
	?>
	<!--Displaying past semester courses-->
	<li><a href="<?echo $homeURL.'?mode=archive'?>">Archive</a> <?php 
	if( $_GET['mode'] == "archive")
	{
		echo "<ul>";
		foreach($archiveCourseResults as $course){
			echo"<li><a href='".$homeURL."?mode=archive&cid=".$course->course_id."&cmd=getCourse'>".$course->name."_".$course->year."</a>";
			$selectedId= $_GET['cid'];
			if(!is_null($selectedId) && $selectedId==$course->course_id)
			{
				echo "<ul>".
						"<li><a href='".$homeURL."?mode=archive&cid=".$selectedId."&cmd=getLecture'>Lectures</a></li>".
						"<li><a href='".$homeURL."?mode=archive&cid=".$selectedId."&cmd=getExercise'>Exercises</a></li>".
						"</ul>";
			}
			echo "</li>";
		}
		echo "</ul>";
	}
	
	?>
	</li>
	<li><a href="<?echo $homeURL.'?mode=staff'?>">Staff</a>
		<?php 
		if( $_GET['mode'] == "staff")
		{
			echo "<ul>";
			foreach($users as $user){
				
				//Getting only editors
				if(get_the_author_meta("user_level",$user->ID)==7)
				{
					echo"<li><a href='".$homeURL."?mode=staff&uid=".$user->ID."'</a>".
					get_the_author_meta("user_firstname",$user->ID).
					" ".get_the_author_meta("user_lastname",$user->ID)."
					</li>";
				}
			}
			echo "</ul>";
		} 
		?>
	</li>
</ul>
</div>
<? 

//wp_list_pages( $args );
//$url = isset ($instance['url']) ? esc_attr($instance['url']) : '';
//echo $url;
	}

	function register(){
		register_sidebar_widget('Widget Course Management', array('Widget_name', 'widget'));
		register_widget_control('Widget Course Management', array('Widget_name', 'control'));
	}
}


//hook the installation function to the activation plugin
register_activation_hook(__FILE__,'install_example');
function install_example(){
	global $wpdb;
	$table = "g_courses";
	$structure = "CREATE TABLE $table (
	course_id INT(9) NOT NULL AUTO_INCREMENT,
	name VARCHAR(200) NOT NULL,
	year VARCHAR(4) NOT NULL,
	semester VARCHAR(20) NOT NULL,
	content TEXT NOT NULL,
	PRIMARY KEY id (course_id)
	);";

	$res = $wpdb->query($structure);

	$table1 = "g_lectures";
	$structure1 = "CREATE TABLE $table1 (
	lecture_id INT(9) NOT NULL AUTO_INCREMENT,
	seq_num INT(9) NOT NULL,
	date DATE NOT NULL,
	description VARCHAR(200) NOT NULL,
	slide VARCHAR(200) NOT NULL,
	slide_url VARCHAR(200) NOT NULL,
	additional VARCHAR(200) NOT NULL,
	additional_url VARCHAR(200) NOT NULL,
	course_id INT(9) NOT NULL,
	PRIMARY KEY id (lecture_id)
	);";

	$res1 = $wpdb->query($structure1);

	$table2 = "g_exercises";
	$structure2 = "CREATE TABLE $table2 (
	exercise_id INT(9) NOT NULL AUTO_INCREMENT,
	seq_num INT(9) NOT NULL,
	date DATE NOT NULL,
	file VARCHAR(200) NOT NULL,
	description VARCHAR(200) NOT NULL,
	solution VARCHAR(200) NOT NULL,
	deadline DATE NOT NULL,
	slide VARCHAR(200) NOT NULL,
	additional VARCHAR(200) NOT NULL,
	course_id INT(9) NOT NULL,
	PRIMARY KEY id (exercise_id)
	);";

	$res2 = $wpdb->query($structure2);

	$table3 = "g_staff_course";
	$structure3 = "CREATE TABLE $table3 (
	staff_courses_id INT(11) NOT NULL AUTO_INCREMENT,
	course_id INT(11) NOT NULL,
	staff_id BIGINT(20) NOT NULL,
	PRIMARY KEY id (staff_courses_id)
	) ;";

	$res3 = $wpdb->query($structure3);


	// Populate table
	//$wpdb->query("INSERT INTO $table(name)
	//    VALUES('Google Bot')");
	//$wpdb->query("INSERT INTO $table(bot_name, bot_mark)
	//    VALUES('Yahoo Slurp', 'yahoo')");
}

//hook the installation function to the activation plugin
register_deactivation_hook(__FILE__, $deleteTables);
{
	/*global $wpdb;
	 $table = "g_staff_course";
	$structure = "DROP TABLE $table;";
	$wpdb->query($structure);

	$table1 = "g_courses";
	$structure1 = "DROP TABLE $table1;";
	$wpdb->query($structure1);

	$table2 = "g_exercises";
	$structure2 = "DROP TABLE $table2;";
	$wpdb->query($structure2);

	$table3 = "g_lectures";
	$structure3 = "DROP TABLE $table3;";
	$wpdb->query($structure3);*/
}

include_once('course_content.php');

//Add shortcoude for coursemanagements
add_shortcode('coursemanagement', 'displayCourseContent');
//Integrating Media Library Uploader
function wp_gear_manager_admin_scripts() {
	wp_enqueue_script('media-upload');
	wp_enqueue_script('thickbox');
	wp_enqueue_script('jquery');
}

function wp_gear_manager_admin_styles() {
	wp_enqueue_style('thickbox');
}


add_action('admin_print_scripts', 'wp_gear_manager_admin_scripts');
add_action('admin_print_styles', 'wp_gear_manager_admin_styles');


//Integrating jqueryui datepicker
function my_admin_init() {
	$pluginfolder = get_bloginfo('url') . '/' . PLUGINDIR . '/' . dirname(plugin_basename(__FILE__));
	wp_enqueue_script('jquery');
	wp_enqueue_script('jquery-ui-core');
	wp_enqueue_script('jquery-ui-datepicker', $pluginfolder . '/js/jquery.ui.datepicker.min.js', array('jquery', 'jquery-ui-core') );
	wp_enqueue_style('jquery.ui.theme', $pluginfolder . '/redmond/jquery-ui-1.8.20.custom.css');
}
add_action('admin_init', 'my_admin_init');

?>






















